import {getBusinessInfo, cacheTools} from 'nc-lightapp-front';

let businessInfo = getBusinessInfo() || {};

import Common from '../../../public/actions/employing/common';

export default class FormAction extends Common{
    constructor(comp) {
        super();
        this.comp = comp;
    }

    // 表单进入编辑态
    toEditStatus = (formValue) => {
        const {props} = this.comp;
        const {form, emp} = props;

        // 表单进入编辑
        form.setFormStatus('card', 'edit');
        // 按钮进入编辑的按钮
        this.update({addPageStatus: 'edit'});

        if(formValue) {
            form.setAllFormValue({
                card: formValue
            });
        }

        if(emp.fromApprove) {
            form.setFormItemsDisabled('card', {
                bill_code: true,
                pk_psnjob: true,
                apply_date: true
            });
        }

        // 展开子表
        form.openArea('psninf');
        form.openArea('tailinfo');
        form.openArea('auditinfo');
    }

    // 表单进入浏览态
    toBrowseStatus = (formValue) => {
        const {props} = this.comp;
        const {form, button, emp, cardPagination} = props;
        let showApproveBtn = !emp.fromApprove;

        // 单据状态值
        let billState = 'init';
        // 单据id
        let billId = '';

        // 表单进入编辑
        form.setFormStatus('card', 'browse');
        // 填充表单的值
        if(formValue) {
            form.setAllFormValue({
                card: formValue
            });
            billId = formValue.rows[0].values['pk_entryapply'].value;
            billState = formValue.rows[0].values['workflow_state'].value;
        }
        else {
            let allFormValue = form.getAllFormValue('card').rows[0].values
            billState = allFormValue['workflow_state'].value;
            billId = allFormValue['pk_entryapply'].value;
        }

        // 按钮进入编辑的按钮
        this.update({
            addPageStatus: 'browse'
        })
        // 隐藏掉一些按钮
            button.setButtonVisible({
                add: showApproveBtn,
                query: false,
                assfunc: true,
                print: true,
                batchadd: false,
                refresh: showApproveBtn,
                callback: ['3'].includes(billState),
                edit: (!showApproveBtn && ['3', '2'].includes(billState)) || ['-1'].includes(billState),
                delete: showApproveBtn && ['-1'].includes(billState),
                copy: showApproveBtn && ['0'].includes(billState),
                submit: showApproveBtn && ['-1', '3'].includes(billState),
                excute: showApproveBtn && !['-1', '0', '2', '3', '102'].includes(billState),
                cardrpt: true,
                send_offer: showApproveBtn && ['1', '4', '5', '6'].includes(billState) && !emp.isApprFlow[billId],
                accept_offer: showApproveBtn && ['1', '4', '5', '6'].includes(billState) && !emp.isApprFlow[billId],
                reject_offer: showApproveBtn && ['1', '4', '5', '6'].includes(billState) && !emp.isApprFlow[billId],
                submit_offer: showApproveBtn && ['5', '6'].includes(billState) && !emp.isApprFlow[billId],
                file: true
            });
        button.setButtonDisabled({
            edit: false,
            delete: false,
            copy: false,
            submit: emp.fromApprove,
            callback: emp.fromApprove,
            assfunc: false,
            print: false,
            output: false
        });
         // 展开子表
         form.openArea('psninf'); 
         form.openArea('tailinfo');
         form.openArea('auditinfo');

         // 设置当前卡片id给卡片页的翻页使用
         setTimeout(() => {
            cardPagination.setCardPaginationId({
                id: billId,
                status: 1
            });
         },1);
    }

    // 进入到新增页面
    toAddPage = async () => {
        const {props} = this.comp;
        const {form, dispatch, emp} = props;

        this.clearFormValue();

        try {
            let res = await dispatch({
                type: 'emp/checkToAddPage',
                payload: {
                    postData: {
                        pk_org: emp.orgValue.refpk
                    }
                }
            });
            if(res.data && res.data.message) {
                this.toast({
                    color: 'danger',
                    content: res.data.message
                });
                return;
            }
            if(res.success) {
                this.update({
                    page: 'add'
                });
                // 清空备注输入框
                res.data.form.head.card.rows[0].values['memo'] = {
                    value: ''
                };
                this.toEditStatus(res.data.form.head.card);

                form.setFormItemsDisabled('card', {
                    bill_code: !res.data.isBillCodeEditable,
                    pk_psnjob: false
                });
            }
        }
        catch(e) {
            console.log('toAddPageError', e);
        }
    }

    // 进入到修改页面
    toEditPage = async (selectedRows, formValue, ...others) => {
        const {props} = this.comp;
        const {form, dispatch, emp} = props;

        this.clearFormValue();

        let postData = {
            billid: selectedRows[0].values['pk_entryapply'].value,
            isapprove: emp.fromApprove,
            pk_org: emp.orgValue ? emp.orgValue.refpk : businessInfo.groupId
        };

        // 如果直接传入form表单的值，就直接进来了
        if(formValue) {
            this.update({
                page: 'edit'
            });
            this.toEditStatus(formValue);
            if(others && 'isBillCodeEditable' in others) {
                form.setFormItemsDisabled('card', {
                    bill_code: !others.isBillCodeEditable,
                    pk_psnjob: true
                });
            }
            return;
        }

        try {
            let res = await dispatch({
                type: 'emp/editBill',
                payload: {
                    postData: postData
                }
            });
            if(!res.success) {
                return;
            }
            if(res.data && res.data.message) {
                this.toast({
                    color: 'danger',
                    content: res.data.message
                });
                return;
            }
            let oneBillData = await this.getOneBillData(selectedRows);

            if(oneBillData === false) {
                return;
            }

            this.update({
                page: 'edit'
            });
            form.setFormItemsDisabled('card', {
                bill_code: !res.data.isBillCodeEditable,
                pk_psnjob: true,
                apply_date: false
            });
            this.toEditStatus(oneBillData.data.head.card);
        }
        catch(e) {
        }
    }

    // 直接进入预览页面
    toBrowsePage = async (selectedRows, formValue) => {
        this.clearFormValue();
        if(formValue) {
            this.toBrowseStatus(formValue);
            return;
        }

        try {
            let oneBillData = await this.getOneBillData(selectedRows);

            if(oneBillData) {
                this.update({
                    page: 'edit'
                });
            }
            this.toBrowseStatus(oneBillData.data.head.card);
        }
        catch(e) {
        }
    }

    // 清空form
    clearFormValue = () => {
        const {props} = this.comp;
        const {form, dispatch, button, emp} = props;

        form.setAllFormValue({
            card: {
                rows: [{
                    values: {
                    }
                }]
            }
        });
    }

    // 获取一条数据
    getOneBillData = async (selectedRows) => {
        const {props} = this.comp;
        const {dispatch, emp} = props;

        let postData = {
            billid: selectedRows[0].values['pk_entryapply'].value,
            pk_org: emp.orgValue ? emp.orgValue.refpk : businessInfo.groupId
        };

        try {
            let res = await dispatch({
                type: 'emp/getOneBillData',
                payload: {
                    postData: postData
                }
            });

            if(res.success) {
                this.update({
                    addPageHistoryData: this.deepCopy(res.data.head.card)
                });

                return res;
            }
        }
        catch(e) {
            console.error(e);
        }

        return false;
    }
}